package com.zzw.springboot.service.impl;


import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zzw.springboot.bean.Inventory;
import com.zzw.springboot.bean.StockChange;
import com.zzw.springboot.bean.SysUser;
import com.zzw.springboot.mapper.InventoryMapper;
import com.zzw.springboot.mapper.StockChangeMapper;
import com.zzw.springboot.mapper.SysUserMapper;
import com.zzw.springboot.service.InventoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 库存服务实现类
 */
@Service
public class InventoryServiceImpl extends ServiceImpl<InventoryMapper, Inventory> implements InventoryService {

    @Autowired
    private InventoryMapper inventoryMapper;

    @Autowired
    private StockChangeMapper stockChangeMapper;

    @Override
    public List<Inventory> getInventoryList(Inventory inventory) {
        return inventoryMapper.selectInventoryList(inventory);
    }

    @Override
    public List<Inventory> getAlertList() {
        return inventoryMapper.selectAlertList();
    }

    @Override
    public Inventory getInventoryByProductAndWarehouse(Integer productId, Integer warehouseId) {
        return inventoryMapper.selectInventoryByProductAndWarehouse(productId, warehouseId);
    }

    @Override
    @Transactional
    public boolean setAlertThreshold(Integer productId, Integer warehouseId, Integer alertThreshold) {
        // 检查库存记录是否存在
        Inventory inventory = inventoryMapper.selectInventoryByProductAndWarehouse(productId, warehouseId);
        if (inventory == null) {
            // 不存在则创建一条新记录
            inventory = new Inventory();
            inventory.setProductId(productId);
            inventory.setWarehouseId(warehouseId);
            inventory.setQuantity(0);
            inventory.setAvailableQuantity(0);
            inventory.setLockedQuantity(0);
            inventory.setAlertThreshold(alertThreshold);
            return inventoryMapper.insertInventory(inventory) > 0;
        } else {
            // 存在则更新
            return inventoryMapper.updateAlertThreshold(productId, warehouseId, alertThreshold) > 0;
        }
    }

    @Override
    @Transactional
    public boolean stockIn(Integer productId, Integer warehouseId, Integer quantity, Integer operatorId,
                          Integer relatedId, Integer relatedType, String remark) {
        if (quantity <= 0) {
            return false;
        }

        // 查询当前库存
        Inventory inventory = inventoryMapper.selectInventoryByProductAndWarehouse(productId, warehouseId);
        int beforeQuantity = 0;

        if (inventory == null) {
            // 不存在则创建新记录
            inventory = new Inventory();
            inventory.setProductId(productId);
            inventory.setWarehouseId(warehouseId);
            inventory.setQuantity(quantity);
            inventory.setAvailableQuantity(quantity);
            inventory.setLockedQuantity(0);
            inventoryMapper.insertInventory(inventory);
        } else {
            // 记录操作前库存
            beforeQuantity = inventory.getQuantity();

            // 增加库存
            inventoryMapper.increaseQuantity(productId, warehouseId, quantity);
        }

        // 记录库存变动
        StockChange stockChange = new StockChange();
        stockChange.setProductId(productId);
        stockChange.setChangeType(1); // 1-入库
        stockChange.setChangeQuantity(quantity);
        stockChange.setBeforeQuantity(beforeQuantity);
        stockChange.setAfterQuantity(beforeQuantity + quantity);
        stockChange.setRelatedId(relatedId);
        stockChange.setRelatedType(relatedType);
        stockChange.setWarehouseId(warehouseId);
        stockChange.setOperatorId(operatorId);
        stockChange.setRemark(remark);

        return stockChangeMapper.insertStockChange(stockChange) > 0;
    }

    @Override
    @Transactional
    public boolean stockOut(Integer productId, Integer warehouseId, Integer quantity, Integer operatorId,
                           Integer relatedId, Integer relatedType, String remark) {
        if (quantity <= 0) {
            return false;
        }

        // 查询当前库存
        Inventory inventory = inventoryMapper.selectInventoryByProductAndWarehouse(productId, warehouseId);
        if (inventory == null || inventory.getAvailableQuantity() < quantity) {
            // 库存不足
            return false;
        }

        // 记录操作前库存
        int beforeQuantity = inventory.getQuantity();

        // 减少库存
        int result = inventoryMapper.decreaseQuantity(productId, warehouseId, quantity);
        if (result <= 0) {
            // 减库存失败
            return false;
        }

        // 记录库存变动
        StockChange stockChange = new StockChange();
        stockChange.setProductId(productId);
        stockChange.setChangeType(2); // 2-出库
        stockChange.setChangeQuantity(quantity);
        stockChange.setBeforeQuantity(beforeQuantity);
        stockChange.setAfterQuantity(beforeQuantity - quantity);
        stockChange.setRelatedId(relatedId);
        stockChange.setRelatedType(relatedType);
        stockChange.setWarehouseId(warehouseId);
        stockChange.setOperatorId(operatorId);
        stockChange.setRemark(remark);

        return stockChangeMapper.insertStockChange(stockChange) > 0;
    }

    @Override
    @Transactional
    public boolean adjustStock(Integer productId, Integer warehouseId, Integer adjustQuantity, Integer operatorId,
                              Integer relatedId, Integer relatedType, String remark) {
        if (adjustQuantity == 0) {
            return false;
        }

        // 查询当前库存
        Inventory inventory = inventoryMapper.selectInventoryByProductAndWarehouse(productId, warehouseId);
        int beforeQuantity = 0;
        int changeType;

        if (adjustQuantity > 0) {
            // 盘盈
            changeType = 3; // 3-盘盈

            if (inventory == null) {
                // 不存在则创建新记录
                inventory = new Inventory();
                inventory.setProductId(productId);
                inventory.setWarehouseId(warehouseId);
                inventory.setQuantity(adjustQuantity);
                inventory.setAvailableQuantity(adjustQuantity);
                inventory.setLockedQuantity(0);
                inventoryMapper.insertInventory(inventory);
            } else {
                // 记录操作前库存
                beforeQuantity = inventory.getQuantity();

                // 增加库存
                inventoryMapper.increaseQuantity(productId, warehouseId, adjustQuantity);
            }
        } else {
            // 盘亏
            changeType = 4; // 4-盘亏

            // 负数转正数用于计算
            int absAdjustQuantity = Math.abs(adjustQuantity);

            if (inventory == null || inventory.getAvailableQuantity() < absAdjustQuantity) {
                return false; // 库存不足
            }

            // 记录操作前库存
            beforeQuantity = inventory.getQuantity();

            // 减少库存
            int result = inventoryMapper.decreaseQuantity(productId, warehouseId, absAdjustQuantity);
            if (result <= 0) {
                return false; // 减库存失败
            }
        }

        // 更新盘点时间
        inventoryMapper.updateLastCheckTime(productId, warehouseId);

        // 记录库存变动
        StockChange stockChange = new StockChange();
        stockChange.setProductId(productId);
        stockChange.setChangeType(changeType);
        stockChange.setChangeQuantity(Math.abs(adjustQuantity)); // 记录绝对值
        stockChange.setBeforeQuantity(beforeQuantity);
        stockChange.setAfterQuantity(beforeQuantity + adjustQuantity);
        stockChange.setRelatedId(relatedId);
        stockChange.setRelatedType(relatedType);
        stockChange.setWarehouseId(warehouseId);
        stockChange.setOperatorId(operatorId);
        stockChange.setRemark(remark);

        return stockChangeMapper.insertStockChange(stockChange) > 0;
    }

    @Override
    @Transactional
    public boolean lockStock(Integer productId, Integer warehouseId, Integer quantity) {
        if (quantity <= 0) {
            return false;
        }

        // 查询当前库存
        Inventory inventory = inventoryMapper.selectInventoryByProductAndWarehouse(productId, warehouseId);
        if (inventory == null || inventory.getAvailableQuantity() < quantity) {
            // 库存不足
            return false;
        }

        // 锁定库存
        return inventoryMapper.lockQuantity(productId, warehouseId, quantity) > 0;
    }

    @Override
    @Transactional
    public boolean unlockStock(Integer productId, Integer warehouseId, Integer quantity) {
        if (quantity <= 0) {
            return false;
        }

        // 查询当前库存
        Inventory inventory = inventoryMapper.selectInventoryByProductAndWarehouse(productId, warehouseId);
        if (inventory == null || inventory.getLockedQuantity() < quantity) {
            // 锁定库存不足
            return false;
        }

        // 解锁库存
        return inventoryMapper.unlockQuantity(productId, warehouseId, quantity) > 0;
    }

    @Override
    public List<StockChange> getStockChangeList(StockChange stockChange) {
        return stockChangeMapper.selectStockChangeList(stockChange);
    }

    @Override
    public List<StockChange> getStockChangeByProductId(Integer productId) {
        return stockChangeMapper.selectStockChangeByProductId(productId);
    }

    @Override
    public List<StockChange> getStockChangeByRelated(Integer relatedId, Integer relatedType) {
        return stockChangeMapper.selectStockChangeByRelated(relatedId, relatedType);
    }
}
